package com.lq.payment.controller;

import com.lq.common.result.Result;
import com.lq.payment.entity.Payment;
import com.lq.payment.service.IPaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 支付记录表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/payments")
@Api(tags = "支付管理", description = "支付相关接口")
public class PaymentController {

    @Autowired
    private IPaymentService paymentService;

    @ApiOperation(value = "创建支付记录", notes = "创建一个新的支付记录")
    @PostMapping("/")
    public Result createPayment(@ApiParam(value = "支付信息", required = true) @RequestBody Payment payment) {
        paymentService.save(payment);
        return Result.success(payment);
    }

    @ApiOperation(value = "根据ID获取支付记录", notes = "根据支付记录ID获取支付详细信息")
    @GetMapping("/{id}")
    public Result getPaymentById(@ApiParam(value = "支付记录ID", required = true) @PathVariable Long id) {
        return Result.success(paymentService.getById(id));
    }

    @ApiOperation(value = "根据订单ID获取支付记录", notes = "根据订单ID获取支付记录")
    @GetMapping("/order/{orderId}")
    public Result getPaymentByOrderId(@ApiParam(value = "订单ID", required = true) @PathVariable Long orderId) {
        Payment payment = paymentService.getByOrderId(orderId);
        return Result.success(payment);
    }

    @ApiOperation(value = "获取所有支付记录", notes = "获取系统中所有的支付记录")
    @GetMapping("/")
    public Result getAllPayments() {
        return Result.success(paymentService.list());
    }

    @ApiOperation(value = "更新支付记录", notes = "根据支付记录ID更新支付信息")
    @PutMapping("/{id}")
    public Result updatePayment(@ApiParam(value = "支付记录ID", required = true) @PathVariable Long id,
                                @ApiParam(value = "支付信息", required = true) @RequestBody Payment payment) {
        payment.setId(id);
        paymentService.updateById(payment);
        return Result.success(payment);
    }

    @ApiOperation(value = "删除支付记录", notes = "根据支付记录ID删除支付记录")
    @DeleteMapping("/{id}")
    public Result deletePayment(@ApiParam(value = "支付记录ID", required = true) @PathVariable Long id) {
        paymentService.removeById(id);
        return Result.success();
    }
}